package com.jh.fcsm.config;

import com.jh.fcsm.filter.TokenFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
import org.springframework.security.web.authentication.logout.LogoutSuccessHandler;

/**
 * spring security配置
 */
@EnableGlobalMethodSecurity(prePostEnabled = true)
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    private AuthenticationSuccessHandler authenticationSuccessHandler;
    @Autowired
    private AuthenticationFailureHandler authenticationFailureHandler;
    @Autowired
    private LogoutSuccessHandler logoutSuccessHandler;
    @Autowired
    private AuthenticationEntryPoint authenticationEntryPoint;
    @Autowired
    private UserDetailsService userDetailsService;
    @Autowired
    private TokenFilter tokenFilter;


    @Bean
    public PasswordEncoder bCryptPasswordEncoder() {
        return new MyPasswordEncoder();
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.csrf().disable();

        // 基于token，所以不需要session
        http.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);

        String [] swaggerPer={"/v2/api-docs/**","/v3/api-docs/**", "/swagger-resources/**","/webjars/**"};

        // 部分资源放行
        http.authorizeRequests()
                .antMatchers("/*.html", "/favicon.ico", "/css/**", "/js/**", "/fonts/**", "/images/**").permitAll()
                .antMatchers(swaggerPer).permitAll()
                .antMatchers( "/statics/**", "/**/**/rest/**").permitAll()
                .antMatchers("/sys/captcha/getCode",
                        "/sys/captcha/checkLoginKaptcha", "/sys/user/uppwd", "/sys/user/bindSer", "/sys/user/qrbindSer").permitAll()
                .anyRequest().authenticated();

        http.formLogin().loginPage("/sys/login").successHandler(authenticationSuccessHandler)
                .failureHandler(authenticationFailureHandler).and().exceptionHandling()
                .authenticationEntryPoint(authenticationEntryPoint);

        http.logout().logoutUrl("/**/logout").logoutSuccessHandler(logoutSuccessHandler);
        // 解决不允许显示在iframe的问题
        http.headers().frameOptions().disable();
        http.headers().cacheControl();
        http.httpBasic().and().rememberMe().rememberMeCookieName("rememberMe").rememberMeParameter("rememberMe");
        http.cors();
        http.addFilterBefore(tokenFilter, UsernamePasswordAuthenticationFilter.class);
    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(userDetailsService).passwordEncoder(bCryptPasswordEncoder());
    }

}
